%%% Calculate Exogenous Growth Gains
clear
rng(17)

results = [];

params = [0.1207	0.0296	0.1584	1.538	7.108	0.938];
max_year = 500;
%%% UNCOMMENT BELOW TWO LINES FOR THE HALF TRADE COST COUNTERFACTUAL
% import_tariff = 1.538;
% import_tariff_counterfactual = 1.269; 
%%% UNCOMMENT BELOW TWO LINES FOR THE NEAR AUTARKY COUNTERFACTUAL
import_tariff = 4;
import_tariff_counterfactual = 1.538; 
beta = 1.03/1.05;

wage_gains_tracker_US = 0;
wage_gains_tracker_OECD = 0;
consumption_gains_tracker_US = 0;
consumption_gains_tracker_OECD = 0;

for repeats = 1:1:10
[wage_levels_counterfactual, real_consumption] = steady_state_simulation_exogenous_gains(params, max_year, import_tariff, import_tariff_counterfactual);

wage_growth_US = geomean(wage_levels_counterfactual(201:299,1)./wage_levels_counterfactual(200:298,1));
wage_growth_OECD = geomean(wage_levels_counterfactual(201:299,2)./wage_levels_counterfactual(200:298,2));
consumption_growth_US = geomean(real_consumption(201:299,1)./real_consumption(200:298,1));
consumption_growth_OECD = geomean(real_consumption(201:299,2)./real_consumption(200:298,2));

baseline_growth = mean([wage_growth_US,wage_growth_OECD,consumption_growth_US,consumption_growth_OECD]);

wage_levels_counterfactual_US = real_consumption(:,1);
wage_levels_counterfactual_OECD = real_consumption(:,2);

wage_levels_baseline_US = real_consumption(1:299,1);
counter = 1;
while counter <= max_year-299
    if import_tariff_counterfactual<1.474
wage_levels_baseline_US(end+1) = wage_levels_baseline_US(end)*1.0301;
    else
wage_levels_baseline_US(end+1) = wage_levels_baseline_US(end)*baseline_growth;
    end
counter = counter+1;
end

wage_levels_baseline_OECD = real_consumption(1:299,2);
counter = 1;
while counter <= max_year-299
    if import_tariff_counterfactual<1.474
wage_levels_baseline_OECD(end+1) = wage_levels_baseline_OECD(end)*1.0301;
    else
wage_levels_baseline_OECD(end+1) = wage_levels_baseline_OECD(end)*baseline_growth;
    end
counter = counter+1;
end

discount_power_vec = 1:max_year-299;
discount_factor_vec = beta.^(discount_power_vec-1)';

PDU_baseline_US = sum(discount_factor_vec.*(log(wage_levels_baseline_US(300:end))));
PDU_counterfactual_US = sum(discount_factor_vec.*(log(wage_levels_counterfactual_US(300:end))));

PDU_baseline_US_temp = PDU_baseline_US;
wage_levels_baseline_US_temp = wage_levels_baseline_US(300:end);
US_EV_multiplier = 1;
while PDU_baseline_US_temp < PDU_counterfactual_US
    US_EV_multiplier = US_EV_multiplier+0.0001;
    wage_levels_baseline_US_temp = wage_levels_baseline_US(300:end).*US_EV_multiplier;
    PDU_baseline_US_temp = sum(discount_factor_vec.*(log(wage_levels_baseline_US_temp)));
end

PDU_baseline_OECD = sum(discount_factor_vec.*(log(wage_levels_baseline_OECD(300:end))));
PDU_counterfactual_OECD = sum(discount_factor_vec.*(log(wage_levels_counterfactual_OECD(300:end))));

PDU_baseline_OECD_temp = PDU_baseline_OECD;
wage_levels_baseline_OECD_temp = wage_levels_baseline_OECD(300:end);
OECD_EV_multiplier = 1;
while PDU_baseline_OECD_temp < PDU_counterfactual_OECD
    OECD_EV_multiplier = OECD_EV_multiplier+0.0001;
    wage_levels_baseline_OECD_temp = wage_levels_baseline_OECD(300:end).*OECD_EV_multiplier;
    PDU_baseline_OECD_temp = sum(discount_factor_vec.*(log(wage_levels_baseline_OECD_temp)));
end

dynamic_gains_US_consumption = US_EV_multiplier
dynamic_gains_OECD_consumption = OECD_EV_multiplier

% wage_gains_tracker_US = wage_gains_tracker_US+dynamic_gains_US_wages;
% wage_gains_tracker_OECD = wage_gains_tracker_OECD+dynamic_gains_OECD_wages;

consumption_gains_tracker_US = consumption_gains_tracker_US+dynamic_gains_US_consumption;
consumption_gains_tracker_OECD = consumption_gains_tracker_OECD+dynamic_gains_OECD_consumption;


end

% wage_gains_US = wage_gains_tracker_US./repeats
% wage_gains_OECD = wage_gains_tracker_OECD./repeats
consumption_gains_US = consumption_gains_tracker_US./repeats
consumption_gains_OECD = consumption_gains_tracker_OECD./repeats

results = [results; import_tariff_counterfactual import_tariff_counterfactual consumption_gains_US consumption_gains_OECD];

%save('exogenous_gains_half_trade_costs.mat','results');
%save('exogenous_gains_near_autarky.mat','results');


